<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="utf-8">
    <title>用户列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/layuimini/lib/layui-v2.6.3/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/layuimini/css/public.css}" media="all">

    <style>
        .flag-span {
            display:inline-block;
            height:25px;
            line-height:25px;
            padding:0 8px;
            border:1px solid transparent;
            color:#fff;
            text-align:center;
            font-size:12px;
            border-radius:2px;
        }
        .flag-span-green {
            background-color:#5FB878;
        }
        .flag-span-orange {
            background-color:#fe9c5b;
        }
        .flag-span-grey {
            background-color:#d3d3d3;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 发布 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="parentOff">
            {{# if (d.parentOff) { }}
            <span class="flag-span flag-span-green"> 追评 </span>
            {{# } else { }}
            <span class="flag-span flag-span-orange"> 父级 </span>
            {{# } }}
        </script>

        <script type="text/html" id="adminOnOrOff">
            {{# if (d.adminOnOrOff == 1) { }}
            <span class="flag-span flag-span-green"> 是 </span>
            {{# } else if (d.adminOnOrOff == 0) { }}
            <span class="flag-span flag-span-orange"> 否 </span>
            {{# } }}
        </script>

        <script type="text/html" id="commentator">
            {{# if (d.commentator == "管理员") { }}
            <span class="flag-span flag-span-green"> {{ d.commentator }} </span>
            {{# } else { }}
            <span class="flag-span flag-span-grey"> {{ d.commentator }} </span>
            {{# } }}
        </script>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="reply">回复</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script th:src="@{/layuimini/lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        function tableRender() {
            table.render({
                elem: '#currentTableId',
                // url: '/layuimini/api/comment.json',
                url: '/admin/comment/selectCommentList',
                toolbar: '#toolbarDemo',
                defaultToolbar: ['filter', 'exports', 'print', {
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],
                parseData: function (res) {
                    return {
                        code: res.code,
                        msg: res.msg,
                        count: res.count,
                        data: res.data.comments
                    }
                },
                cols: [
                    [
                        {type: "checkbox", width: 50},
                        {field: 'id', width: 80, title: 'ID', align: "center"},
                        {field: 'blogName', width: 180, title: '博客标题', align: "center"},
                        {field: 'parentOff', width: 100, title: '是否追评', templet: "#parentOff", align: "center"},
                        {field: 'adminOnOrOff', width: 100, title: '管理员回复', templet: "#adminOnOrOff", align: "center"},
                        {field: 'commentator', width: 100, title: '评论人', templet: "#commentator", align: "center"},
                        {field: 'content', width: 260, title: '评论内容', align: "center"},
                        {field: 'createTime', width: 170, title: '创建时间', align: "center"},
                        {title: '操作', minWidth: 140, toolbar: '#currentTableBar', align: "center"}
                    ]
                ],
                limits: [10, 25, 50, 100],
                limit: 10,
                page: true,
                skin: 'line'
            });
        }

        tableRender()

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '评论发布',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['50%', '40%'],
                    content: '/admin/comment/comment-add',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                var checkCount = checkStatus.data.length
                if (checkCount === 0) {
                    layer.msg('批量删除至少选中一项数据');
                    return false;
                }
                layer.confirm('真的要删除选中的项吗？',function() {
                    var idArrays = [];
                    for (var i = 0; i < checkCount; i++) {
                        idArrays.push(checkStatus.data[i].id)
                    }
                    var ids = ""
                    idArrays.forEach(function (value) {
                        ids += value + ","
                    })
                    ids = ids.substring(0, ids.length - 1)
                    $.get('/admin/comment/deleteComments/' + ids, function (res) {
                        if (res.code === 200) {
                            //刷新表格
                            tableRender()
                            layer.msg(res.msg)
                        } else if (res.code === 444) {
                            layer.msg(res.msg)
                        }
                    })
                })
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'reply') {

                var index = layer.open({
                    title: '评论回复',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['50%', '25%'],
                    content: '/admin/comment/comment-reply/' + data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function () {
                    $.get('/admin/comment/deleteComment/' + data.id, function (res) {
                        if (res.code === 200) {
                            tableRender()
                            layer.msg(res.msg)
                        } else if (res === 444) {
                            layer.msg(res.msg)
                        }
                    })
                });
            }
        });

    });
</script>

</body>
</html>
